import {Component, OnInit} from '@angular/core';
import {ActivatedRoute, Router} from "@angular/router";
import {SHARE_CONFIG} from "@core/config/share-config/share.config";
import {ToolsService} from "@shared/service/tools/tools.service";


@Component({
  selector: 'seed-share',
  templateUrl: './share.component.html',
  styleUrls: ['./share.component.scss']
})
export class ShareComponent implements OnInit {

  constructor(private route: ActivatedRoute,
              private router: Router,
              public tools: ToolsService) {
    this.handleShare();
  }

  handleShare() {
    if (/Mobile/i.test(navigator.userAgent) || this.tools.ua().isWeixin) {
      this.handleShareOnMobile();
    } else {
      this.router.navigate(['/']);
    }
  }

  handleShareOnMobile() {
    this.route.queryParams.subscribe(params => {
      const type = params['type'];
      const path = SHARE_CONFIG.PathMap[type];
      if (path) {
        const url = `${location.origin}/${path}/${params['shareId']}`;
        window.location.replace(url);
      }
    });
  }

  ngOnInit() {
  }
}
